package com.code.leetcode.before50;

import java.util.*;

/**
 * @author codeDorado
 * @version 1.0
 * @date 2020/7/23 10:00
 */
public class The49 {
    public <out> List<List<String>> groupAnagrams(String[] strs) {
        HashMap<String, List<String>> map = new HashMap<>();
        for (String out : strs) {
            char[] chars = out.toCharArray();
            Arrays.sort(chars);
            String str = Arrays.toString(chars);
            if (!map.containsKey(str)) {
                map.put(str, new ArrayList<>());
            }
            map.get(str).add(out);
        }
        return new ArrayList<>(map.values());
    }

    public <out> List<List<String>> groupAnagramsB(String[] strs) {
        if (strs.length == 0) return new ArrayList();
        Map<String, List> ans = new HashMap<String, List>();
        int[] count = new int[26];
        for (String s : strs) {
            Arrays.fill(count, 0);
            for (char c : s.toCharArray()) count[c - 'a']++;

            StringBuilder sb = new StringBuilder("");
            for (int i = 0; i < 26; i++) {
                // sb.append('#');
                sb.append(count[i]);
            }
            String key = sb.toString();
            if (!ans.containsKey(key)) ans.put(key, new ArrayList());
            ans.get(key).add(s);
        }
        return new ArrayList(ans.values());
    }
}
